package com.biz.primus.ms.sms.service;

import com.biz.primus.base.exception.BizSilentException;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.model.misc.exception.ProviderNotAvailableException;
import com.biz.primus.model.misc.exception.SmsExceptions;
import com.biz.primus.model.misc.vo.ChannelVo;
import com.biz.primus.model.misc.vo.ProviderInfoVo;
import com.biz.primus.model.misc.vo.SendTaskQuery;
import com.biz.primus.model.misc.vo.SendTaskVo;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.sms.service.internal.ChannelService;
import com.biz.primus.ms.sms.service.internal.ProviderSelector;
import com.biz.primus.ms.sms.service.internal.SendTaskService;
import com.biz.primus.ms.sms.service.internal.SensitiveWordService;
import com.biz.primus.ms.sms.service.model.ServiceProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author yanweijin
 * @date 2017/6/4
 */
@Service
public class CMPManageService extends AbstractBaseService {

    @Autowired
    private ChannelService channelService;

    @Autowired
    private ProviderSelector providerSelector;

    @Autowired
    private SensitiveWordService sensitiveWordService;

    @Autowired
    private SendTaskService sendTaskService;


    public SpringPageVO<SendTaskVo> queryRecord(SendTaskQuery query) {
        return sendTaskService.query(query);
    }

    public long getBalance(String providerId) {
        ServiceProvider provider = providerSelector.findProvider(providerId);
        try {
            return provider.getBalance();
        } catch (ProviderNotAvailableException e) {
            throw new BizSilentException(SmsExceptions.SERVER_NOT_AVAILABLE);
        }
    }

    public String saveChannel(ChannelVo channelVo) {
        return channelService.saveChannel(channelVo);
    }

    public String updateChannel(ChannelVo channelVo) {
        return channelService.updateChannel(channelVo);
    }

    public void changeChannelState(String channelCode, boolean enable) {
        channelService.changeChannelState(channelCode, enable);
    }

    public List<ChannelVo> getChannelList() {
        return channelService.getChannelList();
    }

    public void selectProvider(String providerId) {
        providerSelector.setCurrentProvider(providerId);
    }

    public List<ProviderInfoVo> getProviderList() {
        return providerSelector.findAllProviders();
    }

    public void updateSensitiveWords(Set<String> words) {
        sensitiveWordService.saveSensitiveWords(words);
    }

    public Set<String> getSensitiveWords() {
        return sensitiveWordService.findAllWords();
    }
}
